<?php

namespace app\admin\command;

use addons\qcloudsms\Qcloudsms;
use app\common\controller\Backend;
use app\admin\model\Room;
use app\admin\model\Store;
use app\admin\model\Card;
use app\admin\model\user\Card as UserCard;
use app\admin\model\Reserve;
use Carbon\Carbon;
use think\Hook;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\Log;

class TestSms extends Command
{
    protected function configure()
    {
        $this->setName('test_sms')
            ->setDescription('test sms notice');
    }

    protected function execute(Input $input, Output $output)
    {
        $yearId = 2024;
        $nowYearId = date('Y');
        $curYearId = $yearId;
        // 往前推13个月份
        // 如果是查当年
        $months = [];
        if ($curYearId == $nowYearId) {
            $endMonth = date('Ym');
        } else {
            $endMonth = date('Ym', strtotime($curYearId . '1201'));
        }
        $endMonth = $endMonth . '01';
        $monthTotal = 13;
        for ($m = 0; $m < $monthTotal; $m++) {
            $monthId = date('Ym', strtotime('-' . $m . ' months', strtotime($endMonth)));
            $months[] = $monthId;
        }
        var_dump($months);
        exit;
        
      $lnglat = [
        'lng'  => '104.042201',
        'lat' => '30.533579',
      ];
      $lng = '104.04445';
        $lat = '30.53161';
        $dis = calDistance($lng, $lnglat['lng'], $lat, $lnglat['lat']) * 1000;
        echo $dis;
        exit;
        /*
        $month = date('Ym');
        $month = $month . "01";
        $monthCarbon = Carbon::parse($month);
        $startDay = $monthCarbon->startOfMonth()->toDateString();
        $endDay = $monthCarbon->endOfMonth()->toDateString();
        echo $startDay . "--" . $endDay;
        $now = Carbon::now();
        // print_r($now);
        $calStartClock = time();
        $calEndClock = 1727400950;
         */

        $startCarbon = Carbon::parse($calStartClock);
        $endCarbon = Carbon::parse($calEndClock);
        // var_dump($startCarbon->toDateTimeString());
        // var_dump($endCarbon->toDateTimeString());
        $diffMinutes = $endCarbon->diffInMinutes($startCarbon);
        // var_dump($diffMinutes);
        $diffFloatHours = $endCarbon->floatDiffInHours($startCarbon);
        var_dump($diffFloatHours);
        var_dump(round($diffFloatHours,1));

        $diffHours = $endCarbon->DiffInHours($startCarbon);
        var_dump($diffHours);
        $mins = ceil(($diffFloatHours - $diffHours) * 60);
        var_dump($mins);
        if ($mins > 30) {
            $diffHours = $diffHours + 0.5;
        }
        var_dump(round($diffHours - 0.5, 1));
        exit;
        // 修改原来的card表的store_ids 内容变为room表的id
        $wheres = [
          'id'  => ['>', 0],
        ];
        $cards = Card::where($wheres)->select();
        foreach ($cards as $card) {
            if (!empty($card->store_ids)) {
                $roomIds = [];
                $storeIds = explode(",", $card->store_ids);
                $rooms = Room::where('store_id', 'in', $storeIds)->order('id', 'asc')->select();
                foreach($rooms as $r) {
                    $roomIds[] = $r->id;
                }
                $roomIdsStr = implode(',', $roomIds);
                echo "card id =>" . $card->id . " --- store ids =>" . $card->store_ids . " --- room ids =>" . $roomIdsStr . PHP_EOL;
                $card->store_ids = $roomIdsStr;
                $card->save();
            }
        }
        $wheres = [
          'id'  => ['>', 0],
        ];
        $userCards = UserCard::where($wheres)->select();
        foreach ($userCards as $card) {
            if (!empty($card->store_ids)) {
                $roomIds = [];
                $storeIds = explode(",", $card->store_ids);
                $rooms = Room::where('store_id', 'in', $storeIds)->order('id', 'asc')->select();
                foreach($rooms as $r) {
                    $roomIds[] = $r->id;
                }
                $roomIdsStr = implode(',', $roomIds);
                echo "user card id =>" . $card->id . " --- store ids =>" . $card->store_ids . " --- room ids =>" . $roomIdsStr . PHP_EOL;
                $card->store_ids = $roomIdsStr;
                $card->save();
            }
        }

        /*
        // 修复出租设置的00问题
        $wheres = [
          // 'id'  => ['=', 16],
          'id'  => ['>', 0],
        ];
        $rooms = Room::where($wheres)->select();
        foreach ($rooms as $room) {
            if (!empty($room->rent_time)) {
                $rentTimeTmp = $room->rent_time;
                $rentTimes = json_decode($rentTimeTmp, true);
                foreach ($rentTimes as &$rentTime) {
                    $start = Carbon::parse(date('Y-m-d') . " " . $rentTime['start']);
                    $end = Carbon::parse(date('Y-m-d') . " " . $rentTime['end']);
                    $diffM = $end->diffInMinutes($start);
                    if ($diffM == 30) {
                        $end->subMinute();
                        echo $rentTime['end'] . "--" . $rentTime['start'] . "--" . $diffM . ' minutes' . ", sub end time => " . $end->toTimeString() . PHP_EOL;
                        $rentTime['end'] = substr($end->toTimeString(), 0, 5);
                        echo $rentTime['end'] . "--" . $rentTime['start']  . PHP_EOL;
                        echo "-------------------------------------------" . PHP_EOL;
                    }
                }
                unset($rentTime);
                $convertRentTime = json_encode($rentTimes);
                $room->rent_time = $convertRentTime;
                $room->save();
            }
        }
        // 查找创建时间大于当前日期的
        $wheres = [
            // 'createtime' => ['>', time()],
            'endtime' => ['>', date('Y-m-d H:i:s')],
        ];
        $reserves = Reserve::where($wheres)->select();
        foreach ($reserves as $reserve) {
            $end = Carbon::parse($reserve->endtime);
            if (substr($end->toDateTimeString(), -4) == '0:00') {
                $end->subMinute();
                echo $reserve->id . "--" . $end->toDateTimeString() . PHP_EOL;
                $reserve->endtime = $end->toDateTimeString();
                $reserve->save();
            }

        }
        */
        /*
        $qcloudsms = new Qcloudsms();
        $params = [
            'mobile'  => '13764628924',
            'template' => '2140717',
            'msg' => [
                'username' => '一路狂奔',
                'ordertime' => '2024',
                'roomname' => '5人小舞室',
                'starttime' => '04-28',
                'endtime' => '04-28',
            ],
        ];
        $check = $qcloudsms->smsNotice($params);
        if ($check) {
          echo "发送成功" . PHP_EOL;
          exit;
        }
        $err = $qcloudsms->getError();
        var_dump($err);
        */

    }

}
    function calDistance($lng1, $lng2, $lat1, $lat2) {
        $earthRadius = 6378.137;
        $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度

        $radLat2=deg2rad($lat2);

        $radLng1=deg2rad($lng1);

        $radLng2=deg2rad($lng2);

        $a=$radLat1-$radLat2;

        $b=$radLng1-$radLng2;

        $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))* $earthRadius;

        return $s;
    }
